<script lang="ts" setup>
import { ref } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { Search } from '@element-plus/icons-vue'
import { ElMessageBox } from 'element-plus'
import { ElMessage } from 'element-plus'
import type { paperListType, PaperDataType } from '@/interface'
import type { SubjectDataType } from '@/interface/modules/subject'
import { getPaperListAPI, delPaperAPI, changePaperStatusAPI } from '@/api/paper'
import { useOptionStore, useUserStore } from '@/stores'
import { formatDate } from '@/utils/format'
import { getSubjectListAPI2 } from '@/api/subject'
import { useI18n } from 'vue-i18n'

const { t } = useI18n()
const route = useRoute()
const router = useRouter()

const optionStore = useOptionStore()
const userStore = useUserStore()
const subjectData = ref<SubjectDataType[]>([])
const paperData = ref<PaperDataType[]>([])
const total = ref(0)
const loading = ref(false)
const paperListParams = ref<paperListType>({
  id: userStore.userData.id as number, // 用户id
  pageIndex: 1,
  pageSize: 10,
  paperType: '',
  subjectId: '',
  keyword: ''
})

// 获取学科列表
const getSubjectList = async () => {
  const { data: res } = await getSubjectListAPI2()
  if (res.status === 200) {
    subjectData.value = res.data
  }
}
getSubjectList()

// 获取试卷列表
const getPaperList = async () => {
  const { data: res } = await getPaperListAPI(paperListParams.value)
  if (res.status === 200) {
    paperData.value = res.data
    total.value = res.total
  }
}
getPaperList()

// 学科创编
const edit = (row: SubjectDataType) => {
  const index = optionStore.tagBar.findIndex(
    (item) => item.items.path == `${route.matched[1].path}/edit`
  )
  if (index !== -1) {
    optionStore.tagBar[index].current = row.id
      ? `${route.matched[1].path}/edit?id=${row.id}`
      : `${route.matched[1].path}/edit`
  }
  router.push({
    path: `${route.matched[1].path}/edit`,
    query: { id: row.id }
  })
}

// 删除试卷
const remove = async (row: SubjectDataType) => {
  ElMessageBox.confirm(
    `${t('PAPER.DELETE_PAPER_CONTENT')}：${row.name}`,
    t('PAPER.DELETE_PAPER_TITLE'),
    {
      type: 'error'
    }
  ).then(async () => {
    const { data: res } = await delPaperAPI(row.id as any)
    if (res.status === 200) {
      ElMessage({
        type: 'success',
        message: res.message
      })
      getPaperList()
    } else {
      ElMessage({
        type: 'error',
        message: res.message
      })
    }
  })
}

// 切换每页条数
const handleSizeChange = (value: number) => {
  paperListParams.value.pageSize = value
  getPaperList()
}

// 切换分页
const handleCurrentChange = (value: number) => {
  paperListParams.value.pageIndex = value
  getPaperList()
}

// 修改试卷状态
const changeStatus = async (row: PaperDataType) => {
  const { data: res } = await changePaperStatusAPI({
    id: row.id as number,
    status: +!row.status
  })
  if (res.status === 200) {
    row.status = +!row.status
    return ElMessage.success(res.message)
  }
  ElMessage.error(res.message)
}

// 答题监测
const handleMonitor = (row: PaperDataType) => {
  if (!row.status) {
    return ElMessage.warning('请先启用试卷')
  }
  window.open(`/monitor/${row.id}`)
}
</script>

<template>
  <div class="paperList-container">
    <div class="paperList-search">
      <el-input
        class="paperList-search-input"
        v-model="paperListParams.keyword"
        :placeholder="$t('PAPER.PAPER_NAME_PLACEHOLDER')"
        :prefix-icon="Search"
        @change="getPaperList"
      />
      <el-select
        class="paperList-search-input"
        v-model="paperListParams.paperType"
        :placeholder="$t('PAPER.PAPER_TYPE_PLACEHOLDER')"
        clearable
        @change="getPaperList"
      >
        <el-option value="1" :label="$t('PAPER.PAPER_OPTION_FIXED')" />
        <el-option value="4" :label="$t('PAPER.PAPER_OPTION_TIME_SLOT')" />
        <el-option value="6" :label="$t('PAPER.PAPER_OPTION_TASK_EXAM')" />
      </el-select>
      <el-select
        class="paperList-search-input"
        v-model="paperListParams.subjectId"
        :placeholder="$t('PAPER.SUBJECT_PLACEHOLDER')"
        clearable
        @change="getPaperList"
      >
        <el-option
          v-for="item in subjectData"
          :key="item.id"
          :value="item.id"
          :label="item.name"
        />
      </el-select>
      <el-button style="margin-right: 1.25rem" type="primary" @click="edit">
        {{ $t('PAPER.BUTTON') }}
      </el-button>
    </div>
    <el-table
      v-loading="loading"
      :data="paperData"
      style="width: 100%; margin-bottom: 1.875rem"
      border
      show-overflow-tooltip
    >
      <el-table-column prop="id" label="ID" width="60" :resizable="false" />
      <el-table-column
        prop="subject_name"
        :label="$t('PAPER.SUBJECT')"
        :resizable="false"
      />
      <el-table-column
        prop="name"
        :label="$t('PAPER.PAPER_NAME')"
        :resizable="false"
      />
      <el-table-column :label="$t('PAPER.CREATE_TIME')" :resizable="false">
        <template #default="{ row }">
          {{ formatDate(row.create_time, 'YYYY-MM-DD HH:mm:ss') }}
        </template>
      </el-table-column>
      <el-table-column
        :label="$t('PAPER.CREATE_USER')"
        width="130"
        :resizable="false"
      >
        <template #default="{ row }">
          <el-tag type="success">{{ row.create_user }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column :label="$t('PAPER.UPDATE_TIME')" :resizable="false">
        <template #default="{ row }">
          {{
            row.update_time
              ? formatDate(row.update_time, 'YYYY-MM-DD HH:mm:ss')
              : '无'
          }}
        </template>
      </el-table-column>
      <el-table-column
        :label="$t('PAPER.UPDATE_USER')"
        width="130"
        :resizable="false"
      >
        <template #default="{ row }">
          <el-tag type="success">{{ row.update_user || '无' }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column :label="$t('PAPER.LIMIT_TIME')" :resizable="false">
        <template #default="{ row }">
          {{
            row.limit_start_time
              ? `${formatDate(row.limit_start_time, 'YYYY-MM-DD')} -- ${formatDate(row.limit_end_time, 'YYYY-MM-DD')}`
              : '无'
          }}
        </template>
      </el-table-column>
      <el-table-column
        :label="$t('PAPER.SCORE')"
        width="120"
        :resizable="false"
      >
        <template #default="{ row }"> {{ row.score }}分 </template>
      </el-table-column>
      <el-table-column
        :label="$t('PAPER.STATUS')"
        width="120"
        :resizable="false"
      >
        <template #default="{ row }">
          <el-tag :type="row.status ? 'success' : 'warning'">
            {{ row.status ? $t('PAPER.ENABLE') : $t('PAPER.DISABLE') }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column
        :label="$t('PAPER.OPERATE')"
        :show-overflow-tooltip="false"
        :resizable="false"
        width="400"
      >
        <template #default="{ row }">
          <el-button
            :type="!row.status ? 'success' : 'warning'"
            size="small"
            plain
            @click="changeStatus(row)"
          >
            {{ !row.status ? $t('PAPER.ENABLE') : $t('PAPER.DISABLE') }}
          </el-button>
          <el-button type="primary" size="small" plain @click="edit(row)">
            {{ $t('PAPER.EDIT') }}
          </el-button>
          <el-button
            type="warning"
            size="small"
            plain
            @click="handleMonitor(row)"
          >
            {{ $t('PAPER.ANSWERING_MONITORING') }}
          </el-button>
          <el-button type="danger" size="small" plain @click="remove(row)">
            {{ $t('PAPER.DELETE') }}
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      :page-sizes="[10, 20, 30]"
      background
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </div>
</template>

<style lang="scss" scoped>
.paperList-container {
  .paperList-search {
    display: flex;
    width: 50%;
    margin-bottom: 1.25rem;
    .paperList-search-input {
      flex: 1;
      margin-right: 0.875rem;
    }
  }
}
</style>
